package com.innover.activemq.producer;

import com.innover.activemq.message.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import javax.jms.*;

/**
 * Created by James on 8/8/2017.
 */
@Component
public class Producer {
    private static final Logger logger = LoggerFactory.getLogger(Producer.class);

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    @Qualifier("standaloneTaskQueue")
    private Queue standaloneTaskQueue;

    @Autowired
    @Qualifier("sequenceTaskQueue")
    private Queue sequenceTaskQueue;

    public boolean sendMessage(MessageModel messageModel){
        try{

            Queue queue = getQueue(messageModel);

            jmsMessagingTemplate.getJmsTemplate().send(queue, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    BytesMessage message = session.createBytesMessage();
                    message.writeBytes(messageModel.getMessageBody());

                    wrapperMessage(messageModel, message);

                    return message;
                }

                private void wrapperMessage(MessageModel messageModel, BytesMessage message) throws JMSException {
                    if (messageModel.getMessageHeader().getMessageSelector() == null || messageModel.getMessageHeader().getMessageSelector().equals("")){

                    } else {
                        message.setStringProperty("QueueSelector", messageModel.getMessageHeader().getMessageSelector());
                    }

                    if (messageModel.getMessageHeader().getMessageGroupId() == null || messageModel.getMessageHeader().getMessageGroupId().equals("")){

                    } else {
                        message.setStringProperty("JMSXGroupID", messageModel.getMessageHeader().getMessageGroupId());
                        if(messageModel.getMessageHeader().isCloseMessageGroup()){
                            message.setIntProperty("JMSXGroupSeq", -1);
                        }
                    }
                }
            });
            return true;
        } catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return false;
        }
    }

    private Queue getQueue(MessageModel messageModel) {
        //0 standalone; 1 sequence
        if (messageModel.getMessageHeader().getQueueType() == 0){
            return standaloneTaskQueue;
        } else {
            messageModel.getMessageHeader().setMessageGroupId("");
            messageModel.getMessageHeader().setMessageSelector("");
            return sequenceTaskQueue;
        }
    }
}
